package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.EasyPoiUtil;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Fine;
import com.jason.system.entity.Reward;
import com.jason.system.entity.SolveOrder;
import com.jason.system.service.FineService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/24 16:37
 */
@RestController
@RequestMapping("/api/fine")
@Api(value = "/fine",tags = "奖惩接口")
public class FineController {

  private static final Logger logger = LoggerFactory.getLogger(FineController.class);

  @Autowired
  private FineService fineService;

  @ApiOperation(value = "奖惩信息",notes = "获取所有奖惩信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page", value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "没页个数",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "获取所有奖惩信息成功"),
          @ApiResponse(code = 201, message = "获取所有奖惩信息失败")
  })
  @GetMapping("/getAll")
  public JSONObject getAllFine(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>FineController==>>>正在获取所有奖惩信息.....");
    Pager<Fine> finePager = fineService.getAllFine(page, size);
    logger.info("==>>>FineController==>>>获取fines对象: {}",finePager);
    if (finePager!=null){
      logger.info("==>>>FineController==>>>获取所有奖惩信息完成.....");
      r.setCode(200);
      r.setMessage("获取所有奖惩信息成功");
      r.setData(finePager);
    }else {
      logger.info("==>>>FineController==>>>获取所有奖惩信息失败.....");
      r.setCode(201);
      r.setMessage("获取所有奖惩信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "奖惩信息",notes = "根据时间段获取所有奖惩信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page", value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "没页个数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "startTime",value = "开始时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "endTime",value = "截止时间",required = true,dataType = "Date")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "根据时间段获取所有奖惩信息成功"),
          @ApiResponse(code = 201, message = "根据时间段获取所有奖惩信息失败")
  })
  @PostMapping("/getAllByTime")
  public JSONObject getAllFineByTime(Integer page, Integer size,Integer sIds, String startTime,String endTime) throws ParseException {
    logger.info("==>>>FineController==>>>进行路径：/api/fine/getAllByTime.....startTime:{}",startTime);
    logger.info("==>>>FineController==>>>进行路径：/api/fine/getAllByTime.....endTime:{}",endTime);
    logger.info("==>>>FineController==>>>进行路径：/api/fine/getAllByTime.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    logger.info("==>>>FineController==>>>根据时间段正在获取所有奖惩信息.....");
    Pager<Fine> finePager;
    if(sIds!=null&&(startTime.equals(endTime))){
      finePager = fineService.getAllFineByTime(page, size,sIds,sdf.parse(startTime),sdf.parse(endTime));
    }else {
      finePager = fineService.getAllFineByTime(page, size,sIds,sdf.parse(startTime),sdf.parse(endTime));
    }
    logger.info("==>>>FineController==>>>获取fines对象: {}",finePager);
    if (finePager!=null){
      logger.info("==>>>FineController==>>>根据时间段获取所有奖惩信息完成.....");
      r.setCode(200);
      r.setMessage("根据时间段获取所有奖惩信息成功");
      r.setData(finePager);
    }else {
      logger.info("==>>>FineController==>>>根据时间段获取所有奖惩信息失败.....");
      r.setCode(201);
      r.setMessage("根据时间段获取所有奖惩信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "添加奖惩信息",notes = "添加奖惩信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "sIds", value = "学生ids,String集合，以逗号分割",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "category",value = "奖惩类别(1-奖励，0-惩罚)",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "money",value = "金钱",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "time",value = "时间",required = true,dataType = "Date")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "添加奖惩信息成功"),
          @ApiResponse(code = 201, message = "添加奖惩信息失败")
  })
  @PostMapping("/insert")
  public JSONObject insertFine(Fine fine){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/insert.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    try {
      logger.info("==>>>FineController==>>>正在添加奖惩信息中.....");
      fineService.insertFine(fine);
      logger.info("==>>>FineController==>>>添加奖惩信息完成.....");
      r.setCode(200);
      r.setMessage("添加奖惩信息成功");
    }catch (Exception e){
      logger.info("==>>>FineController==>>>添加奖惩信息失败.....");
      r.setCode(201);
      r.setMessage("添加奖惩信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "更新奖惩信息",notes = "更新奖惩信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "sIds", value = "学生ids,String集合，以逗号分割",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "category",value = "奖惩类别(1-奖励，0-惩罚)",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "money",value = "金钱",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "time",value = "时间",required = true,dataType = "Date")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "更新奖惩信息成功"),
          @ApiResponse(code = 201, message = "更新奖惩信息失败")
  })
  @PostMapping("/update")
  public JSONObject updateFine(Fine fine){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/update.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    try {
      logger.info("==>>>FineController==>>>更新奖惩信息中.....");
      fineService.updateFine(fine);
      logger.info("==>>>FineController==>>>更新奖惩信息完成.....");
      r.setCode(200);
      r.setMessage("更新奖惩信息成功");
    }catch (Exception e){
      logger.info("==>>>FineController==>>>更新奖惩信息失败.....");
      r.setCode(201);
      r.setMessage("更新奖惩信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "删除奖惩信息",notes = "删除奖惩信息")
  @ApiImplicitParam(paramType = "query",name = "fId",value = "奖惩信息id",required = true,dataType = "Integer")
  @ApiResponses({
          @ApiResponse(code = 200,message = "删除奖惩信息成功"),
          @ApiResponse(code = 201,message = "删除奖惩信息失败")
  })
  @PostMapping("/delete/{fId}")
  public JSONObject deleteFine(@PathVariable("fId") Integer fId){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/delete.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>FineController==>>>正在删除奖惩信息.....");
    boolean result = fineService.deleteFine(fId);
    if (result){
      logger.info("==>>>FineController==>>>删除奖惩信息成功.....");
      r.setCode(200);
      r.setMessage("删除奖惩信息成功");
    }else {
      r.setCode(201);
      r.setMessage("删除奖惩信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "批量删除奖惩信息",notes = "批量删除奖惩信息")
  @ApiImplicitParam(paramType = "query",name = "fIds",value = "奖惩信息ids，以String形式存在，逗号分割",required = true,dataType = "String")
  @ApiResponses({
          @ApiResponse(code = 200,message = "批量删除奖惩信息成功"),
          @ApiResponse(code = 201,message = "批量删除奖惩信息失败")
  })
  @PostMapping("/deleteFines")
  public JSONObject deleteFine(String fIds){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/deleteFines.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>FineController==>>>正在批量删除奖惩信息.....");
    boolean result = fineService.deleteFines(fIds);
    if (result){
      logger.info("==>>>FineController==>>>批量删除奖惩信息成功.....");
      r.setCode(200);
      r.setMessage("批量删除奖惩信息成功");
    }else {
      r.setCode(201);
      r.setMessage("批量删除奖惩信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "获取每个学生总奖罚信息",notes = "获取每个学生总奖惩信息")
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取每个同学总奖惩信息成功"),
          @ApiResponse(code = 201,message = "获取每个同学总奖惩信息失败")
  })
  @GetMapping("/getAllStuReward")
  public JSONObject getAllStuReward(){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/getAllStuReward.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    List<Reward> rewardList = fineService.getAllStuReward();
    logger.info("==>>>FineController==>>>获取的rewards对象为:{}",rewardList);
    if (rewardList!=null){
      logger.info("==>>>FineController==>>>获取每个同学总奖罚信息成功.....");
      r.setCode(200);
      r.setMessage("获取每个同学总惩罚信息成功");
      r.setData(rewardList);
    }else {
      logger.info("==>>>FineController==>>>获取每个同学总奖罚信息失败.....");
      r.setCode(201);
      r.setMessage("获取每个同学总惩罚信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "将学生奖惩总信息导出至Excel",notes = "将学生奖惩总信息导出至Excel")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "body",name = "rewardList",value = "学生奖惩总信息列表",required = true,dataType ="List" )
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "导出每个同学总奖惩信息成功"),
          @ApiResponse(code = 201,message = "导出每个同学总奖惩信息失败")
  })
  @PostMapping("/downLoad")
  public JSONObject downLoad(List<Reward> rewardList , HttpServletResponse response){
    logger.info("==>>>FineController==>>>进行路径：/api/fine/downLoad.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>FineController==>>>获取的rewards对象为:{}",rewardList);
    String fileName = "学生总奖惩信息汇总";
    try {
      logger.info("==>>>WorkLoadController==>>>使用easyPoi工具类将同学总奖惩信息导入至excel中...");
      EasyPoiUtil.defaultExport(rewardList,fileName,"totalReward",fileName+".xlsx",
              SolveOrder.class,response);
      r.setCode(200);
      r.setMessage("导出学生总奖惩信息至excel中成功");
    }catch (Exception e){
      logger.info("==>>>WorkLoadController==>>>使用easyPoi工具类将同学总奖惩信息导入至excel失败:{}",e.getMessage());
      r.setCode(201);
      r.setMessage("导出学生总奖惩信息至excel中失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

}
